﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class CourseController : Controller
    {
        //
        // GET: /Administrator/Course/
        CollegeWebsiteDataContext _datacontext = new CollegeWebsiteDataContext();
        public ActionResult Index()
        {
            List<Course> cou = _datacontext.Courses.Select(c => c).ToList();
            return View(cou);
        }

        //
        // GET: /Administrator/Course/Details/5

        public ActionResult Details(int id)
        {
            Course cou = _datacontext.Courses.Select(c => c).Single(c => (c.CourseID == id));
            return View(cou);
        }

        //
        // GET: /Administrator/Course/Create

        public ActionResult Create()
        {
            List<Department> department = _datacontext.Departments.Select(d => d).Where(d => d.Status).ToList();
            ViewData["department"] = department;
            Course cou = new Course();
            return View(cou);
        } 

        //
        // POST: /Administrator/Course/Create

        [HttpPost]
        public ActionResult Create(Course cou)
        {
            
                if (ModelState.IsValid)
                {
                    _datacontext.Courses.InsertOnSubmit(cou);
                    _datacontext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                // TODO: Add insert logic here
                
          
                return View();
            
        }
        
        //
        // GET: /Administrator/Course/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<Department> department = _datacontext.Departments.Select(d => d).Where(d => d.Status).ToList();
            ViewData["department"] = department;
            Course cou = _datacontext.Courses.Single(c => (c.CourseID == id));
            return View(cou);
        }

        //
        // POST: /Administrator/Course/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Course newcou)
        {
            try
            {
                // TODO: Add update logic here
                Course oldcou = _datacontext.Courses.Single(c => c.CourseID == id);
                oldcou.DepartmentID = newcou.DepartmentID;
                oldcou.CourseName = newcou.CourseName;
                oldcou.CourseCapicity = newcou.CourseCapicity;
                oldcou.CourseFee = newcou.CourseFee;
                oldcou.Status = newcou.Status;

                _datacontext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception exception)
            {
                ViewData["ErrorInfo"] = exception.StackTrace;
                return View("Error");
            }
        }

        //
        // GET: /Administrator/Course/Delete/5
 
        public ActionResult Delete(int id)
        {
            Course cou = _datacontext.Courses.Single(c => c.CourseID == id);
            return View(cou);
        }

        //
        // POST: /Administrator/Course/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Course cou = _datacontext.Courses.Single(c => c.CourseID == id);
                _datacontext.Courses.DeleteOnSubmit(cou);
                _datacontext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch(Exception ex) 
            {
                ViewData["ErrorInfo"] = ex.Message;
                return View("Error");
            }
        }
    }
}
